package org.hzero.data.permission.plugins.helper;

import org.hzero.data.permission.dto.UserInfoDTO;
import org.hzero.data.permission.plugins.function.UserInfoFunction;

/**
 * 设置或获取当前登陆用户的信息
 *
 * @author tianhao.luo@hand-china.com
 */
public class UserDetailHelper {
    public final static ThreadLocal<UserInfoDTO> USER_INFO_DTO_THREAD_LOCAL = ThreadLocal.withInitial(UserInfoDTO::new);

    /**
     * 获取当前线程下的用户信息
     *
     * @return 用户信息
     */
    public static UserInfoDTO getCurrThreadUserInfo() {
        return USER_INFO_DTO_THREAD_LOCAL.get();
    }


    /**
     * 将当前用户信息放入ThreadLocal中
     *
     * @param userInfoFunction 获取用户信息的function
     * @param <T>              占位符
     */
    public static <T> void setCurrThreadUserInfo(UserInfoFunction<T> userInfoFunction) {
        UserInfoDTO userInfoDTO = USER_INFO_DTO_THREAD_LOCAL.get();
        userInfoDTO.setUserId(userInfoDTO.getUserId());
        userInfoDTO.setEmployeeCode(userInfoDTO.getEmployeeCode());
        userInfoDTO.setRoleId(userInfoDTO.getRoleId());
    }

    /**
     * 清除当前线程下的用户信息
     */
    public static void clear(){
        USER_INFO_DTO_THREAD_LOCAL.remove();
    }
}
